unit UDevCtrlAdd;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, UDevCtrlAddEditBase, cxGraphics,
  cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxContainer, cxEdit,
  dxSkinsCore, dxSkinOffice2016Colorful, Vcl.Menus, Data.DB, MemDS, DBAccess,
  Uni, System.ImageList, Vcl.ImgList, cxImageList, Vcl.StdCtrls, cxButtons,
  Vcl.ExtCtrls, cxMaskEdit, cxDropDownEdit, cxTextEdit, cxLabel;

type
  TfrmDevCtrlAdd = class(TfrmDevCtrlAddEditBase)
    procedure btnSaveClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmDevCtrlAdd: TfrmDevCtrlAdd;

implementation

uses
  UCommonFunSqlite, UCommonConst;

{$R *.dfm}

procedure TfrmDevCtrlAdd.btnSaveClick(Sender: TObject);
var
  sSql, sCtrlName, sIP: string;
  iCtrlID, iThemeID: Integer;
begin
  inherited;

  sCtrlName := Trim(edtName.Text);
  if sCtrlName.IsEmpty then
  begin
    MsgBoxDefault(RESDEVCTRLNAMEEMPTY);
    edtName.SetFocus;
    Abort;
  end;

  if checkExistInDB('DevCtrlData', 'CtrlName', sCtrlName) then
  begin
    MsgBoxDefault(RESDEVCTRLNAMEEXIST);
    edtName.SetFocus;
    Abort;
  end;

  sIP := Trim(edtIP.Text);
  if sIP.IsEmpty then
  begin
    MsgBoxDefault(RESDEVCTRLIPEMPTY);
    edtIP.SetFocus;
    Abort;
  end;

  if comTheme.ItemIndex = -1 then
  begin
    MsgBoxDefault(RESDEVCTRLTHEMENOSELECTED);
    edtIP.SetFocus;
    Abort;
  end;

  iThemeID := Integer(comTheme.Properties.Items.Objects[comTheme.ItemIndex]);

  iCtrlID := getMaxRecordNum('DevCtrlData', 'CtrlID') + 1;

  sSql := 'insert into DevCtrlData (CtrlID,CtrlName,CtrlIP,ThemeID) values ('
        + QuotedStr(IntToStr(iCtrlID)) + ','
        + QuotedStr(sCtrlName) + ','
        + QuotedStr(sIP) + ','
        + QuotedStr(IntToStr(iThemeID))
        + ')';

  if ExecSql(sSql) then
  begin
    edtName.Clear;

    SendMessage(FParentHandle, WM_REFRESHDATA, 0, 0);

    MsgBoxDefault(RESSAVESUCCESS);
  end
  else
    MsgBoxDefault(RESSAVEFAIL);
end;

end.
